import sys
import cv2
import numpy as np

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Viedo():
    def __init__(self, capture):
        self.capture = capture
        self.currentFrame = np.array([])

    def captureFrame(self):
        ret, readFrame = self.capture.read()
        return readFrame

    def captureNextFrame(self):
        ret, readFrame = self.capture.read()
        if (ret == True):
            self.currentFrame = cv2.cvtColor(readFrame, cv2.COLOR_BGR2BGRA)

    def convertFrame(self):
        height, width = self.currentFrame.shape[:2]
        img = QImage(self.currentFrame, width, height, QImage.Format_RGB32)
        img = QPixmap.fromImage(img)
        self.previousFrame = self.currentFrame
        return img


class Gui(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setGeometry(250, 80, 800, 600)
        self.setWindowTitle('Test')
        self.video = Viedo(cv2.VideoCapture(0))
        self._timmer = QTimer(self)
        self._timmer.timeout.connect(self.play)
        self._timmer.start(150)
        self.update()
        self.videoFrame = QLabel("VideoCapture")
        self.videoFrame.setAlignment(Qt.AlignCenter)
        self.setCentralWidget(self.videoFrame)

        self.ret, self.capturedFrame = self.video.capture.read()
    
    def play(self):
        self.video.captureNextFrame()
        self.videoFrame.setPixmap(self.video.convertFrame())
        self.videoFrame.setScaledContents(True)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Gui()
    ex.show()
    sys.exit(app.exec())
